/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.common.config.Global;
import com.jeesite.modules.storage.entity.Activity;
import com.jeesite.modules.storage.service.OrderService;
import com.jeesite.modules.storage.common.ServerResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 */
@RestController(value = "apiorder")
@RequestMapping(value = "/api/order")
public class OrderController extends APIController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取价格信息
     * @param memberId 会员Id
     * @param transActivityId 转发Id
     * @param goodsSpecId  商品规格Id
     * @param num 数量
     * @param carts 购物车
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "price")
    public ServerResponse<Map> price(@RequestParam(value="memberId") String memberId,@RequestParam(value="transActivityId",required = false) String transActivityId,@RequestParam(value="goodsSpecId",required = false) String goodsSpecId,@RequestParam(value="num",required = false) Integer num,@RequestParam(value="carts",required = false) String carts,@RequestParam(value="addressId",required = false) String addressId)
    {
        return  ServerResponse.createBySuccess(200,orderService.getPrice(memberId,transActivityId,goodsSpecId,num,getJsonParam(carts),addressId));
    }

    /**
     * 下单
     * @param platform 平台【APP或H5】
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param transActivityId 转发Id
     * @param goodsSpecId  商品规格Id
     * @param num 数量
     * @param remark 备注
     * @param carts 购物车
     * @param receiveId 收件Id【收货地址】
     * @param sendId 寄件Id【收货地址】
     * @param payMethod 支付方式【Alipay或Wechat】
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "submit")
    public ServerResponse<String> submit(@RequestParam(value="platform") String platform,@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId,@RequestParam(value="transActivityId") String transActivityId,@RequestParam(value="goodsSpecId") String goodsSpecId,@RequestParam(value="num") Integer num,@RequestParam(value="remark") String remark,@RequestParam(value="carts",required = false) String carts,@RequestParam(value="receiveId") String receiveId, @RequestParam(value="sendId") String sendId, @RequestParam(value="payMethod") String payMethod) throws Exception {
        Integer code = orderService.submit(platform,transMemberId, memberId,transActivityId,goodsSpecId,num,remark,getJsonParam(carts),receiveId,sendId,payMethod);
        switch (code){
            case 200:
                return  ServerResponse.createBySuccess(code,"下单成功",orderService.getIdByMemberId(transMemberId, memberId));
            default:
                return  ServerResponse.createBySuccess(code,"发生其它异常");
        }
    }

    /**
     * 订单搜索
     * @param memberId 会员Id
     * @param keywords 关键字
     * @param size 每页数量
     * @param page 页码
     * @return 我的订单列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "search")
    @ResponseBody
    public ServerResponse<List<Map>> search(@RequestParam(value="memberId") String memberId,@RequestParam(value="keywords") String keywords, @RequestParam(value="size",defaultValue = "5") Integer size, @RequestParam(value="page",defaultValue = "1") Integer page)
    {
        return  ServerResponse.createBySuccess(200,orderService.getSearchList(memberId,keywords, size,page));
    }

    /**
     * 我的订单
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param identity 标识【all全部 payment待付款 delivery待发货 recipient待收货 complete已完成】
     * @param size 每页数量
     * @param page 页码
     * @return 我的订单列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "query")
    @ResponseBody
    public ServerResponse<List<Map>> query(@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId,@RequestParam(value="identity") String identity, @RequestParam(value="size",defaultValue = "5") Integer size, @RequestParam(value="page",defaultValue = "1") Integer page)
    {
        return  ServerResponse.createBySuccess(200,orderService.getListByMemberId(transMemberId,memberId,identity, size,page));
    }

    /**
     * 取消订单
     * @param orderId 订单Id【订单】
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "cancel")
    public ServerResponse<String> cancel(@RequestParam(value="orderId") String orderId) throws Exception {
        Integer code = orderService.cancel(orderId);
        switch (code){
            case 200:
                return  ServerResponse.createBySuccess(code,"订单已取消");
            default:
                return  ServerResponse.createBySuccess(code,"发生其它异常");
        }
    }

    /**
     * 删除订单
     * @param orderId 订单Id【订单】
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "remove")
    public ServerResponse<String> remove(@RequestParam(value="orderId") String orderId) throws Exception {
        Integer code = orderService.remove(orderId);
        switch (code){
            case 200:
                return  ServerResponse.createBySuccess(code,"订单已删除");
            default:
                return  ServerResponse.createBySuccess(code,"发生其它异常");
        }
    }

    /**
     * 确认收货
     * @param orderId 订单Id【订单】
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "recipient")
    public ServerResponse<String> recipient(@RequestParam(value="orderId") String orderId)
    {
        Integer code = orderService.recipient(orderId);
        switch (code){
            case 200:
                return  ServerResponse.createBySuccess(code,"订单已完成");
            default:
                return  ServerResponse.createBySuccess(code,"发生其它异常");
        }
    }

    /**
     * 订单详情
     * @param orderId 订单Id【订单】
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "content")
    public ServerResponse<Map> content(@RequestParam(value="orderId") String orderId)
    {
        return  ServerResponse.createBySuccess(200,orderService.getContentById(orderId));
    }
}
